package com.gk.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.gk.bean.GuoYaoBackDto.ZxyGoodsstockGuoyao;
import com.gk.bean.GuoYaoBackDto.ZxyStockadjustGuoyao;
import com.gk.bean.qfdto.GKT400BackJson;
import com.gk.bean.qfdto.GKT500BackJson;
import com.gk.dao.ZxyGoodsstockGuoyaoDao;
import com.gk.http.entity.HttpResult;
import com.gk.http.service.GetGkDataService;
import com.gk.service.StockService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class StockServiceImpl implements StockService {
    @Resource
    private GetGkDataService getGkDataService;
    @Resource
    private ZxyGoodsstockGuoyaoDao zxyGoodsstockGuoyaoDao;

    @Override
    public void addGkGoodsStocksToQF(String str) {
        HttpResult gkGoodsStocksData = getGkDataService.getGkGoodsStocksData();
        log.info("获取国控库存数据" + JSON.toJSONString(gkGoodsStocksData));
        if (gkGoodsStocksData.getCode().equals("0")) {
            List<GKT500BackJson> gkt500BackJsons = JSONArray.parseArray(gkGoodsStocksData.getData(), GKT500BackJson.class);
            for (GKT500BackJson gkt500BackJson : gkt500BackJsons) {
                List<ZxyGoodsstockGuoyao> zxyGoodsstockGuoyaos = JSONArray.parseArray(JSON.toJSONString(gkt500BackJson.getXml().getContent().getDetail().getSub().getSubline()), ZxyGoodsstockGuoyao.class);
                for (ZxyGoodsstockGuoyao zxyGoodsstockGuoyao : zxyGoodsstockGuoyaos) {
                    zxyGoodsstockGuoyao.setBillno(gkt500BackJson.getXml().getContent().getDetail().getMain().getBillno());
                    zxyGoodsstockGuoyao.setProddate(zxyGoodsstockGuoyao.getProddate().substring(0,10));
                    zxyGoodsstockGuoyao.setValiddate(zxyGoodsstockGuoyao.getValiddate().substring(0,10));
                    int i = zxyGoodsstockGuoyaoDao.insertSelective(zxyGoodsstockGuoyao);
                    log.info("将国药库存数据插入到千方中成功:" + i);
                }
            }
        } else {
            log.info("暂时未能拉取到国药药库存数据！");
        }
    }
}
